package com.taoyibao.hongcanting.ui.admin.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventUpdataTask;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.PageModel;
import com.taoyibao.hongcanting.model.TaskModel;
import com.taoyibao.hongcanting.model.TaskPageModel;
import com.taoyibao.hongcanting.ui.adapter.TaskAdapter;
import com.taoyibao.hongcanting.utils.LogUtils;
import com.taoyibao.hongcanting.widget.RefreshRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 已完成的任务
 * @date :${DATA} 16:04
 */
public class CompletedTaskFragment extends Fragment implements OnRefreshListener, OnRefreshLoadMoreListener {

    private int mStatus;//任务状态
    private List<TaskModel> mData;
    private TaskAdapter mAdapter;
    private RefreshRecyclerView mRefreshRecyclerView;
    private int mCurrentPage = 1;
    private int mMaxPageSize = 10;
    /**
     * -1刷新  1加载更多
     */
    private int mAction = -1;
    private PageModel mPageModel;//分页的数据

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.base_list_delegate, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        mStatus = getArguments().getInt(CodeConstant.STATUS);
        mRefreshRecyclerView = view.findViewById(R.id.refreshRecyclerView);

        mRefreshRecyclerView.getSmartRefreshLayout().setOnRefreshLoadMoreListener(this);

        mData = new ArrayList<>();
        mAdapter = new TaskAdapter(mData, getContext());
        mRefreshRecyclerView.getRecyclerView().setAdapter(mAdapter);
        getGiftOrderList();
    }


    /**
     * @param status 当前任务状态
     * @return
     */
    public static CompletedTaskFragment getInstance(int status) {
        Bundle bundle = new Bundle();
        bundle.putInt(CodeConstant.STATUS, status);
        CompletedTaskFragment fragment = new CompletedTaskFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 获取数据
     */
    public void getGiftOrderList() {
        JsonApiWrapper.getApiService().getGiftOrderList(SpUtils.getToken(), mStatus, mCurrentPage, mMaxPageSize)
                .compose(new DefaultTransformer<CompanyBaseModel<TaskPageModel>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<TaskPageModel>>(getContext(), true) {
                    @Override
                    public void onNext(CompanyBaseModel<TaskPageModel> result) {
                        if (result.code == NetCode.HTTP_200 && result.data != null) {
                            mPageModel = result.data.page_info;
                            List<TaskModel> orders_info = result.data.orders_info;
                            if (mAction == -1) {
                                mData.clear();
                            }
                            mData.addAll(orders_info);
                            if (mData != null && mData.size() > 0) {//有数据
                                mRefreshRecyclerView.setDataView();
                                mAdapter.notifyDataSetChanged();
                            } else {//没有数据
                                mRefreshRecyclerView.setEmptyView();
                            }
                        } else {
                            super.onNext(result);
                        }
                        mRefreshRecyclerView.setRefreshFinish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mAction == -1) {//刷新的时候
                            mRefreshRecyclerView.setEmptyView();
                        }
                        mRefreshRecyclerView.setRefreshFinish();
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 收到更新的消息 刷新页面就好
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdataTask(EventUpdataTask event) {
        LogUtils.dNormal("已完成", "收到消息");
        mAction = -1;
        mCurrentPage = 1;
        getGiftOrderList();
    }

    /**
     * 刷新操作
     *
     * @param refreshLayout
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mAction = -1;
        mCurrentPage = 1;
        getGiftOrderList();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mAction = 1;
        if (mCurrentPage >= mPageModel.total_page) {//已经是最后一页了没有更多数据
            mRefreshRecyclerView.getSmartRefreshLayout().setNoMoreData(true);//不在显示加载更多
            mRefreshRecyclerView.getSmartRefreshLayout().finishLoadMore();
        } else {
            mCurrentPage++;
            getGiftOrderList();
        }
    }


}
